import { ref } from 'vue'
export default function usePaginationStandard(callback) {
  let pagination = ref({ current: 1, pageSize: 10, total: 0 })
  let loading = ref(false)
  let otherQueryStringPrev = ''
  async function call({ current, pageSize, ...otherQuery } = {}) {
    loading.value = true
    //判断otherQueryString是否有变化来决定current是否需要初始化到第一页
    let otherQueryString = JSON.stringify(otherQuery)
    if (otherQueryString !== otherQueryStringPrev) {
      current = 1
    }
    otherQueryStringPrev = otherQueryString
    //从业务回调中获取翻页数据
    let result = await callback({ current, pageSize, ...otherQuery })
    if (result) {
      pagination.value.current = result.current || current
      pagination.value.pageSize = result.pageSize
      pagination.value.total = result.total
    }
    loading.value = false
  }
  return { call, pagination, loading }
}
